﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Students.Bases;

namespace Students
{
    public partial class Courses : BasePage
    {
        private static int CurrentPage = 0;
        private static int CurrentSize = 20;
        private static int TotalPages = 0;
        private BLCourses courses;
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (IsAuthenticated)
            {
                divUser.Visible = true;
                lblCurrentUser.Text = "Hi, " + AuthenticateInfo.Firstname + " " + AuthenticateInfo.Lastname + " | Age: " + AuthenticateInfo.Age;
            }
            else
            {
                Response.Redirect(ResolveUrl("~/Login.aspx"));
                divUser.Visible = false;
            }

            courses = new BLCourses();
            if (!IsPostBack)
            {
                memberUsers.Visible = true;
                memberSection.Visible = false;
                divAlert.Visible = false;
                LoadList();
            }

        }

        private void LoadList()
        {
            if (!String.IsNullOrEmpty(Request.QueryString["p"]))
            {
                bool result = int.TryParse(Request.QueryString["p"], out CurrentPage);
                if (result == false)
                {
                    CurrentPage = 0;
                }
                else
                {
                    CurrentPage--;
                }
            }
            grdViewMemberHistory.DataSource = courses.GetCourses(AuthenticateInfo.Id, CurrentPage, CurrentSize, out TotalPages);
            grdViewMemberHistory.DataBind();
            divPaging.InnerHtml = RenderPaging(CurrentPage);
        }
        protected void grdViewMemberHistory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblNo = (Label)e.Row.FindControl("lblNo");
                lblNo.Text = Convert.ToString(CurrentPage * CurrentSize + e.Row.RowIndex + 1);
            }
        }

        protected string RenderPaging(int current)
        {
            if (current > TotalPages)
            {
                return "";
            }
            string html = "<ul class=\"pagination\">";

            if (current == 0)
            {
                html +=
                    "<li class=\"first disabled\"><a href=\"#\"><< First</a></li>" +
                        "<li class=\"prev disabled\"><a href=\"#\">< Prev</a></li>";
            }
            else
            {
                html += "<li class=\"first\"><a href=\"" + ResolveUrl("~/Register.aspx?p=" + 1) + "\"><< First</a></li>" +
                        "<li class=\"prev\"><a href=\"" + ResolveUrl("~/Register.aspx?&p=" + current) + "\">< Previous</a></li>";
            }

            for (int i = 1; i <= TotalPages; i++)
            {
                if (current == (i - 1))
                {
                    html += "<li class=\"active\"><a href=\"#\">" + i + "</a></li>";
                }
                else
                {
                    html += "<li><a href='" + ResolveUrl("~/Register.aspx&p=" + i) + "'>" + i + "</a></li>";
                }
            }

            if (current < (TotalPages - 1))
            {
                html += "<li class=\"next\"><a href=\"" + ResolveUrl("~/Register.aspx&p=" + (current + 2)) + "\">Next ></a></li>" +
                        "<li class=\"last\"><a href=\"" + ResolveUrl("~/Register.aspx&p=" + TotalPages) + "\">Last >></a></li>";
            }
            else
            {
                html += "<li class=\"next disabled\"><a href=\"#\">Next ></a></li>" +
                        "<li class=\"last disabled\"><a href=\"#\">Last >></a></li>";
            }
            html += "</ul>";
            return html;
        }

        protected void AlertWarning(string msg)
        {
            divAlert.Attributes["class"] = "alert alert-danger";
            divAlert.InnerHtml = msg;
            divAlert.Visible = true;
        }

        protected void AlertError(string msg)
        {
            divAlert.Attributes["class"] = "alert alert-danger";
            divAlert.InnerHtml = msg;
            divAlert.Visible = true;
        }

        protected void AlertSuccess(string msg)
        {
            divAlert.Attributes["class"] = "alert alert-success";
            divAlert.InnerHtml = msg;
            divAlert.Visible = true;
        }

        protected void ClearText()
        {
            tbCity.Text = string.Empty;
            tbConfirmPassword.Text = string.Empty;
            tbCreditNo.Text = string.Empty;
            tbCVS.Text = string.Empty;
            tbEmail.Text = string.Empty;
            tbExpirationDate.Text = string.Empty;
            tbFirstName.Text = string.Empty;
            tbLastName.Text = string.Empty;
            tbPassword.Text = string.Empty;
            tbState.Text = string.Empty;
            tbStreet.Text = string.Empty;
            tbUsername.Text = string.Empty;
            tbZipcode.Text = string.Empty;
        }

    }
}